package com.orhon.smartcampus.modules.core.service;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.baseDao.BaseDaoImpl;
import com.orhon.smartcampus.modules.systemctl.dao.OperationApiDao;
import com.orhon.smartcampus.modules.systemctl.dao.OperationUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.OperationApi;
import com.orhon.smartcampus.modules.systemctl.entity.OperationUserRelation;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * 给定一个用户   根据ID查询出来权限ID
 *
 *
 * 权限ID查询接口ID
 *
 *
 *
 *
 */


@Component
public class AuthDao extends BaseDaoImpl {

    @Autowired
    OperationUserRelationDao operationUserRelationDao;
    @Autowired
    OperationApiDao operationApiDao;






    @Transactional
    public List<Object> findOperationList(Object userId) {
        StringBuffer sb = new StringBuffer();
        if (userId == null){
            sb.append("select user_id from systemctl_operation_user_relation where deleted_at is null group by user_id");

        }else {
            sb.append("select operation_id from systemctl_operation_user_relation where deleted_at is null and user_id = "+userId);

        }
        Session session = this.getSession();
        Query query = session.createSQLQuery(sb.toString());
        //System.out.println("=======执行查询方法======");
        //System.out.println("asdfasdf"+query.list());
//        List list = query.list();
//        List<Map<String,Object>> t = new ArrayList<>();
//        for (Object o : list) {
//            Map<String,Object>map = new HashMap<>();
//            map.put("id",o);
//            t.add(map);
//
//        }
       // System.out.println("List<Map<String,Object>>:"+t);
        return query.list();
    }

   @Transactional
    public List<Object> findApi(Object o) {
        StringBuffer sb = new StringBuffer();
        sb.append("select api_code from systemctl_operation_api where operation_id = "+ o);
        Session session = this.getSession();
        Query query = session.createSQLQuery(sb.toString());
//        List<Map<String,Object>> t = new ArrayList<>();
//        List list = query.list();
//        for (Object code : list) {
//           Map<String,Object>map = new HashMap<>();
//           map.put("api_code",code);
//          // System.out.println(" Map<String,Object> API:"+map);
//           t.add(map);
//        }
        return query.list();
    }







    @Transactional
    public List<OperationUserRelation> findOperationList1(Object userId) {
        List<OperationUserRelation>systemctlUserRetation=new ArrayList<>();
        if(userId==null){
            String hql1="from OperationUserRelation where deletedAt is null group by userUsersByUserId";
            systemctlUserRetation= operationUserRelationDao.find(hql1,null);
        }else{
            String hql = "from OperationUserRelation where deletedAt is null and userUsersByUserId ="+userId;
            systemctlUserRetation=operationUserRelationDao.find(hql,null);
        }
        return systemctlUserRetation;
    }



    @Transactional
    public List<OperationApi> findApi1(Object o) {
        String hql = "from OperationApi where operation_id = "+o;
        List<OperationApi> operationApis = operationApiDao.find(hql);
        return operationApis;
    }




}
